Spot-On
spot-on-mceliece.h
1 /*
2 ** Copyright (c) 2011 - 10^10^10, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Software based on specifications provided by Antoon Bosselaers,
6 ** RenĂ© Govaerts, Robert McEliece, Bart Preneel, Marek Repka,
7 ** Christopher Roering, Joos Vandewalle.
8 **
9 ** Redistribution and use in source and binary forms, with or without
10 ** modification, are permitted provided that the following conditions
11 ** are met:
12 ** 1. Redistributions of source code must retain the above copyright
13 ** notice, this list of conditions and the following disclaimer.
14 ** 2. Redistributions in binary form must reproduce the above copyright
15 ** notice, this list of conditions and the following disclaimer in the
16 ** documentation and/or other materials provided with the distribution.
17 ** 3. The name of the author may not be used to endorse or promote products
18 ** derived from skein without specific prior written permission.
19 **
20 ** SPOT-ON IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ** SPOT-ON, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32 #ifndef _spoton_mceliece_h_
33 #define _spoton_mceliece_h_
34 
35 #ifdef SPOTON_MCELIECE_ENABLED
36 #include <NTL/GF2E.h>
37 #include <NTL/GF2EX.h>
38 #include <NTL/GF2EXFactoring.h>
39 #include <NTL/GF2X.h>
40 #include <NTL/GF2XFactoring.h>
41 #include <NTL/mat_GF2.h>
42 #include <NTL/mat_ZZ_p.h>
43 #include <NTL/vec_GF2E.h>
44 
45 #include <limits>
46 #include <sstream>
47 #include <vector>
48 
49 class spoton_mceliece_private_key
50 {
51  public:
52  enum Conversions
53  {
54  FOA = 0, // Fujisaki-Okamoto Model A
55  FOB, // Fujisaki-Okamoto Model B
56  PCA, // Pointcheval Model A
57  ZZZ
58  };
59 
60  spoton_mceliece_private_key(const char *privateKey,
61  const size_t privateKeyLength);
62  spoton_mceliece_private_key(const size_t m, const size_t t);
63  ~spoton_mceliece_private_key();
64 
65  Conversions conversion(void) const
66  {
67  return m_conversion;
68  }
69 
70  NTL::GF2EX X(void) const
71  {
72  return m_X;
73  }
74 
75  NTL::GF2EX gZ(void) const
76  {
77  return m_gZ;
78  }
79 
80  NTL::mat_GF2 G(void) const
81  {
82  return m_G;
83  }
84 
85  NTL::mat_GF2 P(void) const
86  {
87  return m_P;
88  }
89 
90  NTL::mat_GF2 Pinv(void) const
91  {
92  return m_Pinv;
93  }
94 
95  NTL::mat_GF2 S(void) const
96  {
97  return m_S;
98  }
99 
100  NTL::mat_GF2 Sinv(void) const
101  {
102  return m_Sinv;
103  }
104 
105  NTL::vec_GF2E L(void) const
106  {
107  return m_L;
108  }
109 
110  bool ok(void) const
111  {
112  return m_ok;
113  }
114 
115  bool prepareG(const NTL::mat_GF2 &R);
116 
117  size_t k(void) const
118  {
119  return m_k;
120  }
121 
122  size_t m(void) const
123  {
124  return m_m;
125  }
126 
127  size_t n(void) const
128  {
129  return m_n;
130  }
131 
132  size_t preSynTabSize(void) const
133  {
134  return m_preSynTab.size();
135  }
136 
137  size_t t(void) const
138  {
139  return m_t;
140  }
141 
142  std::vector<NTL::GF2EX> preSynTab(void) const
143  {
144  return m_preSynTab;
145  }
146 
147  std::vector<long int> swappingColumns(void) const
148  {
149  return m_swappingColumns;
150  }
151 
152  void swapSwappingColumns(const size_t i, const size_t j);
153 
154  private:
155  Conversions m_conversion;
156  NTL::GF2EX m_X;
157  NTL::GF2EX m_gZ;
158  NTL::mat_GF2 m_G;
159  NTL::mat_GF2 m_P;
160  NTL::mat_GF2 m_Pinv;
161  NTL::mat_GF2 m_S;
162  NTL::mat_GF2 m_Sinv;
163  NTL::vec_GF2E m_L;
164  bool m_ok;
165  size_t m_k;
166  size_t m_m;
167  size_t m_n;
168  size_t m_t;
169  std::vector<NTL::GF2EX> m_preSynTab;
170  std::vector<long int> m_swappingColumns;
171  bool prepareP(void);
172  bool preparePreSynTab(void);
173  bool prepareS(void);
174  bool prepare_gZ(void);
175  void prepareSwappingColumns(void);
176  void reset(const bool ok);
177 };
178 
179 class spoton_mceliece_public_key
180 {
181  public:
182  spoton_mceliece_public_key(const size_t m, const size_t t);
183  spoton_mceliece_public_key(const size_t t, const NTL::mat_GF2 &Gcar);
184  ~spoton_mceliece_public_key();
185 
186  NTL::mat_GF2 Gcar(void) const
187  {
188  return m_Gcar;
189  }
190 
191  bool ok(void) const
192  {
193  return m_ok;
194  }
195 
196  bool prepareGcar(const NTL::mat_GF2 &G,
197  const NTL::mat_GF2 &P,
198  const NTL::mat_GF2 &S);
199 
200  size_t k(void) const
201  {
202  return static_cast<size_t> (m_Gcar.NumRows());
203  }
204 
205  size_t n(void) const
206  {
207  return static_cast<size_t> (m_Gcar.NumCols());
208  }
209 
210  size_t t(void) const
211  {
212  return m_t;
213  }
214 
215  private:
216  NTL::mat_GF2 m_Gcar;
217  bool m_ok;
218  size_t m_t;
219  void reset(const bool ok);
220 };
221 
222 class spoton_mceliece
223 {
224  public:
225  explicit spoton_mceliece(const QByteArray &pk);
226  spoton_mceliece(const char *privateKey,
227  const size_t privateKeyLength,
228  const QByteArray &publicKey);
229  spoton_mceliece(const QByteArray &conversion,
230  const size_t m,
231  const size_t t);
232  ~spoton_mceliece();
233  bool decrypt(const std::stringstream &ciphertext,
234  std::stringstream &plaintext) const;
235  bool encrypt(const char *plaintext,
236  const size_t plaintext_size,
237  std::stringstream &ciphertext) const;
238  bool generatePrivatePublicKeys(void);
239 
240  bool ok(void) const
241  {
242  if(m_privateKey && m_publicKey)
243  return m_privateKey->ok() && m_publicKey->ok();
244  else
245  return false;
246  }
247 
248  size_t m(void) const
249  {
250  return m_m;
251  }
252 
253  size_t t(void) const
254  {
255  return m_t;
256  }
257 
258  static double expansion(const size_t m, const size_t t)
259  {
260  size_t k = 0;
261  size_t n = static_cast<size_t> (1) << m; // 2^m
262 
263  if(m * t < n)
264  k = n - m * t;
265 
266  if(k > 0)
267  return static_cast<double> (n) / static_cast<double> (k);
268  else
269  return 0.0;
270  }
271 
272  void initializeSystemParameters(const size_t m, const size_t t)
273  {
274  m_m = m;
275  m_n = static_cast<size_t> (1) << m_m; // 2^m
276  m_t = t;
277 
278  /*
279  ** Some calculations.
280  */
281 
282  if(m_m * m_t < m_n)
283  m_k = m_n - m_m * m_t;
284  else
285  m_k = 0;
286  }
287 
288  void privateKeyParameters(QByteArray &privateKey) const;
289  void publicKeyParameters(QByteArray &publicKey) const;
290 
291  private:
292  spoton_mceliece_private_key::Conversions
293  m_conversion; /*
294  ** 000 - None
295  ** foa - Fujisaki-Okamoto Model A
296  ** fob - Fujisaki-Okamoto Model B
297  ** pca - Pointcheval Model A
298  */
299  spoton_mceliece_private_key *m_privateKey;
300  spoton_mceliece_public_key *m_publicKey;
301  size_t m_k;
302  size_t m_m;
303  size_t m_n;
304  size_t m_t;
305 };
306 
307 #endif
308 #endif